package com.proyecto.track.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.proyecto.track.bo.Account;
import com.proyecto.track.bo.Rol;
import com.proyecto.track.bo.UserPerson;
import com.proyecto.track.bo.UserType;
import com.proyecto.track.service.RolService;
import com.proyecto.track.service.UserPersonService;
import com.proyecto.track.service.UserTypeService;

public class UserPersonAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private long id;
	private boolean creandoCuenta;
	private UserTypeService userTypeService;
	private UserPersonService userPersonService;
	private RolService rolService;
	private UserPerson userPerson = new UserPerson();
	private UserType userType = new UserType();
	private Long idRol; 
	private List<UserType> userTypes = new ArrayList<UserType>();
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	private List<UserPerson> userPersons = new ArrayList<UserPerson>();
	private String msg = "";
	
	public String loadUserTypes(){
		userTypes = userTypeService.searchUserType();
		return "SUCCESS";
	}
	
	public String saveUserPerson(){
		if(userPerson.getId() == null){
			userPerson.getAccount().setEnabled(true);
			this.id = userPersonService.createUserPerson(userPerson);
		}else{
			if(idRol != null && idRol != 0){
				userPerson = userPersonService.searchUserPerson(userPerson.getId());
				Rol rol = rolService.search(idRol);
				Account account = userPersonService.searchUserPerson(userPerson.getId()).getAccount();
				account.setRol(rol);
				userPerson.setAccount(account);
			}
			this.id = userPersonService.modifyUserPerson(userPerson);
		}
		return "SUCCESS";
	}
	
	public String deleteUserPerson(){
		userPersonService.deleteUserPerson(id);
		return "SUCCESS";
	}

	public String listUserPersons(){
		userPersons = userPersonService.searchUser();
		return "SUCCESS";
	}
	
	public String searchUserPerson(){
		userPerson = userPersonService.searchUserPerson(id);
		return "SUCCESS";
	}
	
	public String modifyUserPerson(){
		this.searchUserPerson();
		this.loadUserTypes();
		return "SUCCESS";
	}
	
	public String newUserType(){
		
		userTypeService.createUserType(userType);
		return "SUCCESS";
	}
	
	
	public UserPerson getUserPerson() {
		return userPerson;
	}

	public void setUserPerson(UserPerson userPerson) {
		this.userPerson = userPerson;
	}

	public List<UserType> getUserTypes() {
		return userTypes;
	}

	public void setUserTypes(List<UserType> userTypes) {
		this.userTypes = userTypes;
	}

	public List<UserPerson> getUserPersons() {
		return userPersons;
	}

	public void setUserPersons(List<UserPerson> userPersons) {
		this.userPersons = userPersons;
	}

	public void setUserTypeService(UserTypeService userTypeService) {
		this.userTypeService = userTypeService;
	}

	public void setUserPersonService(UserPersonService userPersonService) {
		this.userPersonService = userPersonService;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public UserType getUserType() {
		return userType;
	}

	public void setUserType(UserType userType) {
		this.userType = userType;
	}

	public UserTypeService getUserTypeService() {
		return userTypeService;
	}

	public UserPersonService getUserPersonService() {
		return userPersonService;
	}

	public Long getIdRol() {
		return idRol;
	}

	public void setIdRol(Long idRol) {
		this.idRol = idRol;
	}

	public RolService getRolService() {
		return rolService;
	}

	public void setRolService(RolService rolService) {
		this.rolService = rolService;
	}
	
	public boolean isCreandoCuenta() {
		return creandoCuenta;
	}

	public void setCreandoCuenta(boolean creandoCuenta) {
		this.creandoCuenta = creandoCuenta;
	}

	public void validate() {
		if(creandoCuenta){
			if(userPersonService.searchAccount(userPerson.getAccount().getUserName())!= null){
				addActionError("El nombre de usuario ya existe");
				this.msg = "Username already exist";
			}
			this.msg = "User created";
		}
	}
	
}
